const {getReverseByUsername, newReverse, delReverse, getReverseListByCoachId, updateReverse} = require("../controller/reverse");
const Response = require('../common/Response')
const router = require('koa-router')()

router.prefix('/api/reverse')

router.get('/detail', async (context, next) => {
  const row = await getReverseByUsername(context.session.username)
  if (row.identify) {
    context.body = Response.success(row)
  } else {
    context.body = Response.failure()
  }
})

router.post('/new', async (context, next) => {
  const val = await newReverse(context.request.body)
  if (val) {
    context.body = Response.success('预约练车时间成功')
  } else {
    context.body = Response.failure('预约练车时间失败')
  }
})

router.post('/del/:id', async (context, next) => {
  const val = await delReverse(context.params.id)
  if (val) {
    context.body = Response.success('删除预约练车时间成功')
  } else {
    context.body = Response.failure('删除预约练车时间失败')
  }
})

router.post('/update', async (context, next) => {
  const val = await updateReverse(context.request.body)
  if (val) {
    context.body = Response.success('更新学员预约练车时间成功')
  } else {
    context.body = Response.failure('更新学员预约练车时间失败')
  }
})

// 未用到
router.get('/list/:coach_id', async (context, next) => {
  const rows = await getReverseListByCoachId(context.params.coach_id)
  context.body = Response.success(rows)
})

module.exports = router
